
#pragma once

/*
 * Classes for the Observer Pattern for use with Threads
 * The subject is a thread.
 *
 * author Gerald Gaboury
 * $Id: ThreadedObserver.hpp 2 2012-05-22 17:55:16Z gerry@gaboury.biz $
 */

#include <set>
#include <map>
#include <string>

#include <boost/thread.hpp>

using namespace std;
using namespace boost;

class ThreadedObserverSubject;

class ThreadedObserver
{
protected:
  ThreadedObserverSubject* m_subject;
public:
  ThreadedObserver ( ThreadedObserverSubject* p_subject );
  virtual ~ThreadedObserver();
  virtual void detach();
  virtual void update(int ith) = 0;
};

typedef set<ThreadedObserver*>                        ThreadedObserverSet;
typedef ThreadedObserverSet::iterator                 ThreadedObserverSetIter;

typedef map<string,string>                    StringMap;
typedef map<string,string>::iterator          StringMapIter;
typedef map<string,string>::const_iterator    StringMapCIter;

class ThreadedObserverSubject
{
private:
  ThreadedObserverSet m_observers;
public:
  // default constructor is OK but need to define a destructor
  virtual ~ThreadedObserverSubject();
  virtual void attach ( ThreadedObserver* p_observer );
  virtual void detach ( ThreadedObserver* p_observer );
  virtual void notify (int ith);
};

